package com.niit.lcy.areaselref;

import com.niit.util.DataConvUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Author: Chase
 * @Date: 2024/12/10
 */

public class AreaSelRefMapper extends Mapper<LongWritable, Text, Text, Text> {
    private Text outk = new Text();
    private Text outv = new Text();

    /**
     * map阶段的核心业务逻辑（按行读取，获面积和价格，组合成kv，然后输出）
     *
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        if (key.get() == 0) // 跳过标题行
            return;

        String lineData = value.toString();
        String[] datas = lineData.split("\t");
        if (datas.length >= 5) {
            String areaStr = datas[5].toString();
            String[] areaStrArr = areaStr.split("㎡");
            double area = DataConvUtils.strToDouble(areaStrArr[0], 0);
            if (area > 190) {
                outk.set("大于190㎡");
            } else if (area >= 151 && area <= 190) {
                outk.set("151-190㎡");
            } else if (area >= 121 && area <= 150) {
                outk.set("121-150㎡");
            } else if (area >= 91 && area <= 120) {
                outk.set("91-120㎡");
            } else if (area >= 61 && area <= 90) {
                outk.set("61-90㎡");
            } else if (area > 0 && area <= 60) {
                outk.set("0-60㎡");
            }
//            outk.set();
            outv.set(datas[2]);
            if (area == 0)
                outk.set(datas[5].toString()); // area  total_price
            context.write(outk, outv);
        }
    }
}
